    program merge_episodes_robinhood
    character*5 ticker(5000),tick(3079100),ftick(200)
    character*8 cusp(5000)
    character*7 nhold(3079100),numhld(5000)
    character*9 temp9
    character*24 raw
    integer(kind=8) nshrs(5000),nvalue(5000),jvalue(5000),ivol(5000),numshrs(5000)
    dimension jast(5000),jdate(5000),itickt(5000),age(5000),favg(5000),fmax(5000),fmin(5000),fstd(5000),iperm(5000)
    dimension  price(5000),ishrout(5000),vwret(5000),util(5000),retrn(5000),dfee(5000),nep(5000),ibgtck(200),ndate(3079100)
!      open(unit=10,file='e:\FIS_duration\robinhood\all_robinhood.txt')
!      open(unit=11,file='e:\FIS_duration\episode_data.txt')
!      open(unit=99,file='e:\FIS_duration\episode_data_with_robinhood.txt')
      open(unit=10,file='e:\FIS_duration\robinhood\all_robinhood.txt')
      open(unit=11,file='e:\FIS_duration\episode_data_rep.txt')
      open(unit=99,file='e:\FIS_duration\episode_data_with_robinhood_rep.txt')
      nt=0
      do 5 n=1,3079001
!      read(10,1000)tick(n),ndate(n)
!      write(6,1000)tick(n),ndate(n)
      read(10,1000)tick(n),ndate(n),nhold(n)
      write(6,1000)tick(n),ndate(n),nhold(n)
!      read(10,1000)raw
!      write(6,1000)raw
!1000  format(a24)      
1000  format(1x,a5,1x,i8,1x,a7)      
!1000  format(1x,a5,1x,i8)      
      if(n.eq.1) go to 3
      nm=n-1
      if(tick(n).eq.tick(nm)) go to 5
3     nt=nt+1
      ftick(nt)=tick(n)
      ibgtck(nt)=n
5     continue      
      i=0
10    i=i+1      
      read(11,1100,end=900)nep(i),jast(i),jdate(i),cusp(i),ticker(i),itickt(i),numshrs(i),age(i),favg(i),fmax(i),fmin(i),fstd(i),iperm(i),price(i),ivol(i),retrn(i),ishrout(i),vwret(i),util(i),nshrs(i)     
1100  format(1x,i4,2(1x,i8),1x,a8,2x,a5,5x,i9,1x,i14,5(1x,f11.4),1x,i5,1x,f9.2,1x,i10,1x,f10.6,1x,i9,1x,f9.6,1x,f6.2,1x,i13) 
      if(nep(i).eq.nep(1)) go to 10
20    icnt=i-1      
      if(ticker(1).eq.'     ') go to 500
      numhld='     -9'
      cnth=0
      nn=0
30    nn=nn+1
      if(nn.gt.nt) go to 500
      if(ticker(1).ne.ftick(nn)) go to 30
      np=nn+1
      kend=ibgtck(np)-1
      if(nn.ge.nt) kend=3079001
      do 60 ii=1,icnt
          kk=ibgtck(nn)-1
40        kk=kk+1
          if(kk.gt.kend) go to 60
          if(ndate(kk).lt.jdate(ii)) go to 40
          if(ndate(kk).gt.jdate(ii)) go to 60
          numhld(ii)=nhold(kk)
          cnth=cnth+1.
          go to 40
60    continue
      if(cnth.lt.1.) go to 500
      do 70 ii=1,icnt
70        write(99,9900)nep(ii),jast(ii),jdate(ii),cusp(ii),ticker(ii),itickt(ii),numshrs(ii),age(ii),favg(ii),iperm(ii),price(ii),ivol(ii),retrn(ii),ishrout(ii),vwret(ii),util(ii),nshrs(ii),numhld(ii)     
9900  format(1x,i4,2(1x,i8),1x,a8,2x,a5,5x,i9,1x,i14,2(1x,f11.4),1x,i5,1x,f9.2,1x,i10,1x,f10.6,1x,i9,1x,f9.6,1x,f6.2,1x,i13,1x,a7) 
        
          
      
      
500   jast(1)=jast(i)
      jdate(1)=jdate(i)
      cusp(1)=cusp(i)
      ticker(1)=ticker(i)
      itickt(1)=itickt(i)
      numshrs(1)=numshrs(i)
      jvalue(1)=jvalue(i)
      age(1)=age(i)
      favg(1)=favg(i)
      iperm(1)=iperm(i)
      price(1)=price(i)
      ivol(1)=ivol(i)
      retrn(1)=retrn(i)
      ishrout(1)=ishrout(i)
      vwret(1)=vwret(i)
      util(1)=util(i)
      nshrs(1)=nshrs(i)
      nvalue(1)=nvalue(i) 
      dfee(1)=dfee(i)
      nep(1)=nep(i)
      numhld='     -9'
      i=1
      go to 10
900   iend=iend+1
      if(iend.eq.1) go to 20 
      stop

    end program merge_episodes_robinhood
